package com.now.printer.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.now.printer.R;
import com.now.printer.adapter.RecentlyFileListAdapter;
import com.now.printer.db.RecentlyTable;
import com.now.printer.ui.activity.FilePreviewAndPrintActivity;
import com.now.printer.utils.DbUtils;

import org.xutils.DbManager;
import org.xutils.common.util.LogUtil;
import org.xutils.db.Selector;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

@Deprecated
public class CollectedFileFragment extends Fragment {
    public static final String TITLE = "title";
    private String mTitle = "Defaut Value";
    private RecyclerView mRecyclerView;
    // private TextView mTextView;

    private List<RecentlyTable> recentlyTableList;
    private  RecentlyFileListAdapter recentlyFileListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(TITLE);
        }
        try {
            DbManager db = x.getDb(DbUtils.daoConfig);
            recentlyTableList = db.selector(RecentlyTable.class).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab, container, false);
        mRecyclerView = (RecyclerView) view
                .findViewById(R.id.id_stickynavlayout_innerscrollview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),
                LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.i("onResume");

        recentlyFileListAdapter = new RecentlyFileListAdapter(getActivity(), recentlyTableList, R.layout.item_recently_result_list,2);
        mRecyclerView.setAdapter(recentlyFileListAdapter);
        recentlyFileListAdapter.setOnItemClickLitener(new RecentlyFileListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, String type) {
                LogUtil.i("按下的item name是 ： " + recentlyTableList.get(position).getName());
                LogUtil.i("按下的item path是 ： " + recentlyTableList.get(position).getPath());
                Intent intent = new Intent();
                intent.setClass(getActivity(), FilePreviewAndPrintActivity.class);
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_FILE_URI, recentlyTableList.get(position).getPath());
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_FILE_NAME, recentlyTableList.get(position).getName());
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_FILE_IMAGE, recentlyTableList.get(position).getImageId());
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_FILE_LAST_TIME, recentlyTableList.get(position).getLastTime());
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_FILE_SIZE, recentlyTableList.get(position).getSize());
                startActivity(intent);
            }

            @Override
            public void onItemClickCollect(int position,boolean isCollect) {
                x.task().run(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            DbManager db = x.getDb(DbUtils.daoConfig);

                            Selector<RecentlyTable> recentlyTableSelector = db.selector(RecentlyTable.class)
                                    .where("path","=",recentlyTableList.get(position).getPath());
                            if (recentlyTableSelector.count() > 0 || recentlyTableSelector.findAll().size() > 0 || recentlyTableSelector.getLimit() > 0){
                                LogUtil.i("查询到的最近文档是否存在getLimit ：" + recentlyTableSelector.getLimit());
                                LogUtil.i("查询到的最近文档是否存在findAll ：" + recentlyTableSelector.findAll());
                                LogUtil.i("查询到的最近文档是否存在 count ：" + recentlyTableSelector.count());
                                RecentlyTable recentlyTable = new RecentlyTable();
                                recentlyTable.setIsCollected(String.valueOf(isCollect));
                                db.saveOrUpdate(recentlyTable);

                            }else {
                                LogUtil.i("最近列表没有这个文件， 因此无法收藏");

                            }
                        } catch (DbException e) {
                            e.printStackTrace();
                            LogUtil.i("saveCurrentFileToDatabase DbException：" + e.getMessage());

                        }
                    }
                });

            }
        });
    }


    public static CollectedFileFragment newInstance(String title) {
        CollectedFileFragment tabFragment = new CollectedFileFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TITLE, title);
        tabFragment.setArguments(bundle);
        return tabFragment;
    }

}
